﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Resources
{
    /// <summary>
    /// This class manages the resource loading and readyness. 
    /// It will implement asynchronous assets loading
    /// </summary>
    public class ResourceManager
    {

        public enum ResourcePool
        {
            GameState,
            Global
        }

        private GraphicsDevice graphicsDevice = null;

        private List<ILoadableResource> resources = null;

        private ContentManager globalContentManager = null;

        /// <summary>
        /// Constructor
        /// </summary>
        public ResourceManager(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;
            this.resources = new List<ILoadableResource>();
            this.globalContentManager = new ContentManager(Engine.Instance.Services);
        }

        /// <summary>
        /// Register a loadable resource
        /// </summary>
        internal void registerResource(ILoadableResource newResource)
        {
            resources.Add(newResource);
        }

        public int TotalPendantResources
        {
            get { return this.resources.Count; }
        }

        public int LoadedResources { private set; get; }

        /// <summary>
        /// Load all the resources
        /// </summary>
        public void LoadPendantResources(ContentManager loader)
        {
            LoadedResources = 0;

            //load all objects
            foreach (ILoadableResource loadable in this.resources)
            {
                if (!loadable.IsReady)
                {
                    if (loadable.ResourcePool == ResourcePool.GameState)
                    {
                        loadable.LoadContents(this.graphicsDevice, loader);
                    }
                    else if (loadable.ResourcePool == ResourcePool.Global)
                    {
                        loadable.LoadContents(this.graphicsDevice, this.globalContentManager);
                    }
                    this.LoadedResources++;
                }
            }

            //clear the pendant resources list
            this.resources.Clear();
        }

        bool pendantGlobalPoolRelease = false;

        /// <summary>
        /// Mark the global asset tool as pendat for release.
        /// It will be unloaded in the next gamestate change.
        /// </summary>
        public void MarkGlobalPoolForRelease()
        {
            this.pendantGlobalPoolRelease = true;
        }

        /// <summary>
        /// Check if there is a pendant clearment of the global asset pool
        /// </summary>
        internal void UnloadGlobalPoolIfPendant()
        {
            if (this.pendantGlobalPoolRelease)
            {
                this.globalContentManager.Unload();
                this.pendantGlobalPoolRelease = false;
            }
        }

    }

}
